package com.example.ziru.service.impl;

import com.example.ziru.entity.Rooms;
import com.example.ziru.mapper.RoomsMapper;
import com.example.ziru.service.RoomsService;
import com.example.ziru.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class RoomsServiceImpl implements RoomsService {
    @Autowired
    private RoomsMapper roomsMapper;
    @Override
    public List<Rooms> getAllRooms() {
        return roomsMapper.getAllRooms();
    }

    //查询所有
    @Override
    public Result queryRooms(String column, Integer pageIndex, Integer pageSize) {
        if(pageIndex == null){
            pageIndex = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex,pageSize);
        Page<Rooms> rooms = (Page)roomsMapper.getAllRooms();
        long total = rooms.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rooms",rooms);
        return  new Result("200","查詢成功" ,map);
    }

    @Override
    public Result queryRoomsByPriceAsc(String column, Integer pageIndex, Integer pageSize) {
        if(pageIndex == null){
            pageIndex = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex,pageSize);
        Page<Rooms> rooms = (Page)roomsMapper.queryRoomsByPriceAsc();
        long total = rooms.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rooms",rooms);
        return  new Result("200","查詢成功" ,map);
    }

    @Override
    public Result queryRoomsByAreaAsc(String column, Integer pageIndex, Integer pageSize) {
        if(pageIndex == null){
            pageIndex = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex,pageSize);
        Page<Rooms> rooms = (Page)roomsMapper.queryRoomsByAreaAsc();
        long total = rooms.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rooms",rooms);
        return  new Result("200","查詢成功" ,map);
    }

    @Override
    public Object queryRoomsById(Integer id) {
        if(id == null){
            return new Result("-1","id为空",null);
        }
        Rooms rooms = roomsMapper.queryRoomsById(id);
        if(rooms == null){
            return new Result("-2","数据库查询出错",null);
        }
        return new Result("200","成功",rooms);
    }

    @Override
    public Object select(String column,Integer pageIndex,Integer pageSize,String str) {
        if(!StringUtils.hasLength(str)){
            return new Result("-1","参数为空",null);
        }
        if(pageIndex == null){
            pageIndex = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex,pageSize);
        Page<Rooms> rooms = (Page)roomsMapper.select(str);
        if(rooms == null){
            return new Result("-2","未查询到相关内容",null);
        }
        long total = rooms.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rooms",rooms);
        return  new Result("200","查詢成功" ,map);
    }

    @Override
    public Object queryRoomsByRand() {
        List<Rooms> roomsList = roomsMapper.queryRoomsByRand();
        if(roomsList == null){
            return new Result("-1","数据库查询出错",null);
        }
        return new Result("200","成功",roomsList);
    }

    @Override
    public Object queryTitleByRoomId(Integer id) {
        List<String> str = roomsMapper.queryTileByRoomId(id);
        if(str == null){
            return new Result("-1","查询数据库失败",null);
        }
        return new Result("200","成功",str);
    }
}
